package com.future.bantang.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;
import com.future.bantang.R;
import com.future.bantang.activity.SearchActivity;
import com.future.bantang.adapter.GrowGrassAdapter;
import com.future.bantang.constants.Constants;
import com.future.bantang.entity.SquareEntity;
import com.future.bantang.utils.JsonUtils;
import com.future.bantang.utils.NetUtils;
import com.future.bantang.view.ListViewUp;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by liufujie on 16-1-12.
 */
public class SquareFragment extends BaseFragment {

    //TODO 项目完成删除
    private static final String TAG = "SquareFragment";

    // 控件列表
    @ViewInject(R.id.psts_recommend)
    private com.future.bantang.view.PagerSlidingTabStrip pstsRecommend;

    @ViewInject(R.id.vp_recommend_show)
    private ViewPager vpRecommendShow;

    @ViewInject(R.id.lv_grow_grass)
    private ListViewUp lvGrowGrass;

    //viewpager适配器
    private MyPagerAdapter pagerAdapter;

    //种草小分队列表
    private GrowGrassAdapter grassAdapter;

    //热门推荐列表
    private List<SquareEntity.DataEntity.ModuleElementsEntity> moduleElementsEntityList;

    //种草小分队列表
    private List<SquareEntity.DataEntity.RecGroupsEntity> recGroupsEntityList;

    //种草小分队子列表
    private List<SquareEntity.DataEntity.RecGroupsEntity> subRecGroupsEntityList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_square, null);
        ViewUtils.inject(this, view);
        return view;
    }

    /**
     * 初始化数据
     *
     * @param view
     */
    @Override
    public void initView(View view) {
        moduleElementsEntityList = new ArrayList<>();
        recGroupsEntityList = new ArrayList<>();
        subRecGroupsEntityList = new ArrayList<>();

        copy0 = new ArrayList<>();
        copy1 = new ArrayList<>();

        grassAdapter = new GrowGrassAdapter(getActivity());
    }

    /**
     * 加载半塘广场数据
     */
    @Override
    public void loadDatas() {

        NetUtils.RequestParamsBuilder builder = new NetUtils.RequestParamsBuilder();
        RequestParams params = builder.addPostParameter("client_id", "bt_app_android").
                addPostParameter("client_secret", "ffcda7a1c4ff338e05c42e7972ba7b8d").
                build();

        NetUtils.getSquare(params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                SquareEntity squareEntity = null;
                try {
                    squareEntity = JsonUtils.perse(responseInfo.result, SquareEntity.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                //热门推荐
                moduleElementsEntityList = squareEntity.getData().getModule_elements();

                pagerAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());

                vpRecommendShow.setAdapter(pagerAdapter);

                pstsRecommend.setViewPager(vpRecommendShow);

                //种草小分队
                recGroupsEntityList = squareEntity.getData().getRec_groups();

                copy0.addAll(recGroupsEntityList);

                subRecGroupsEntityList = recGroupsEntityList.subList(0, 5);

                grassAdapter.setDatas(subRecGroupsEntityList);

                lvGrowGrass.setAdapter(grassAdapter);

            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(getActivity(), "加载数据失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick(R.id.sv_square_search_good)
    public void search(View view){
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        startActivity(intent);
    }

    /**
     * 切换listview
     */
    private List<SquareEntity.DataEntity.RecGroupsEntity> copy0;
    private List<SquareEntity.DataEntity.RecGroupsEntity> copy1;
    private int i = 0;


    /**
     * 切换种草小分队
     *
     * @param view
     */
    @OnClick(R.id.ll_change_show)
    public void changeShow(View view) {
        if (recGroupsEntityList.size() == 0 || recGroupsEntityList == null) {
            return;
        }

        if (i % 2 == 0) {
            i %= 2;
            Collections.rotate(copy0, 4);
            copy1 = new ArrayList<>();
            copy1.addAll(copy0);

            subRecGroupsEntityList = copy0.subList(0, 5);

            grassAdapter.setDatas(subRecGroupsEntityList);

            i++;
        } else {
            i %= 2;
            Collections.rotate(copy1, 4);
            copy0 = new ArrayList<>();
            copy0.addAll(copy1);

            subRecGroupsEntityList = copy1.subList(0, 5);
            grassAdapter.setDatas(subRecGroupsEntityList);
            i++;
        }
    }

    /**
     * viewpager的适配器
     */
    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return Constants.TITLES[position];
        }

        @Override
        public Fragment getItem(int position) {
            return RecommendFragment.newInstance(moduleElementsEntityList.get(position));
        }

        @Override
        public int getCount() {
            return Constants.TITLES.length;
        }
    }
}
